import tensorflow as tf

def acc(y_true, y_pred):
    y_pred = tf.sigmoid(y_pred)
    return tf.reduce_mean(tf.cast(tf.equal(y_true, tf.round(y_pred)), tf.float32), axis=-1)

def pre(y_true, y_pred):
    y_pred = tf.sigmoid(y_pred)
    TP = tf.reduce_sum(tf.multiply(y_true, tf.round(y_pred)))
    S = tf.reduce_sum(tf.round(y_pred))
    return TP / (S + 1e-7)  # 加 epsilon 防止除以 0

def rec(y_true, y_pred):
    y_pred = tf.sigmoid(y_pred)
    TP = tf.reduce_sum(tf.multiply(y_true, tf.round(y_pred)))
    S = tf.reduce_sum(y_true)
    return TP / (S + 1e-7)

def _to_tensor(x, dtype):
    return tf.convert_to_tensor(x, dtype=dtype)
